package com.banciyuan.bcywebview.biz.ask.question;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.dialog.ChooseDialog;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Ask;

/**
 * Created by hfxt2008 on 14-7-30.
 */
public class QuestionListviewAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;

    private List<Ask> alist = new ArrayList<>();

    public QuestionListviewAdapter(Context mContext, List<Ask> alist) {
        this.mContext = mContext;
        this.alist = alist;
        this.mInflater = LayoutInflater.from(this.mContext);
    }

    @Override
    public int getCount() {
        return alist.size();
    }

    @Override
    public Object getItem(int postion) {
        return alist.get(postion);
    }

    @Override
    public long getItemId(int postion) {
        return postion;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder mViewHolder = null;
        Ask mItem = alist.get(position);
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.question_item, viewGroup, false);
            mViewHolder = newViewHolder(mViewHolder, convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        mViewHolder.time.setText(StringUtil.getOppositeTime(mItem.getCtime()));
        if (mItem.getAnonymous() == 1) {
            mViewHolder.name.setText(mContext.getString(R.string.anonymous_user));
            mViewHolder.avatar.setImageResource(R.drawable.user_pic_anonymous);
        } else {
            mViewHolder.name.setText(mItem.getCuname());
            ImageLoader.getInstance().displayImage(mItem.getCavatar(), mViewHolder.avatar, ImageOptions.optionHead);
        }
        if (!TextUtils.isEmpty(mItem.getContent())) {
            mViewHolder.content.setText(Html.fromHtml(mItem.getContent()));
        }
        OnClick groundOnClick = new OnClick(position);
        mViewHolder.aciontLayout.setOnClickListener(groundOnClick);
        mViewHolder.acceptLayout.setOnClickListener(groundOnClick);
        mViewHolder.name.setOnClickListener(groundOnClick);
        mViewHolder.avatar.setOnClickListener(groundOnClick);

        return convertView;
    }

    private ViewHolder newViewHolder(ViewHolder mViewHolder, View convertView) {
        mViewHolder.avatar = (ImageView) convertView.findViewById(R.id.civ_ask_head_pic);
        mViewHolder.time = (TextView) convertView.findViewById(R.id.question_item_time_tv);
        mViewHolder.name = (TextView) convertView.findViewById(R.id.question_item_name_tv);
        mViewHolder.content = (TextView) convertView.findViewById(R.id.question_item_intro_tv);
        mViewHolder.aciontLayout = convertView.findViewById(R.id.ask_action);
        mViewHolder.acceptLayout = convertView.findViewById(R.id.question_item_accept_view);
        return mViewHolder;
    }

    private static class ViewHolder {
        TextView time;
        TextView name;
        TextView content;
        ImageView avatar;
        View aciontLayout;
        View acceptLayout;
    }

    public void updateData() {
        notifyDataSetChanged();
    }

    class OnClick implements OnClickListener {

        int position;
        Ask mItem;

        public OnClick(int position) {
            this.position = position;
            this.mItem = (Ask) getItem(position);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ask_action:
                    ChooseDialog.Builder builder = new ChooseDialog.Builder(mContext);
                    builder.setChooseOneString(mContext.getString(R.string.cruel_refuse))
                            .setChooseOneClick(new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ((QuestionActivity) mContext).doQuestionDel(position);
                                }
                            });
                    if (mItem.getAnonymous() == 1) {
                        builder.setChooseTwoString(mContext.getString(R.string.forbiden_anonymous_user_ask))
                                .setChooseTwoClick(new OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ((QuestionActivity) mContext).setAskAnonymous();
                                    }
                                });
                    }
                    if (!TextUtils.isEmpty(mItem.getCuname()) && mItem.getAnonymous() == 0 && !StringUtil.notNullEqual(mItem.getCuid(),
                            UserDataHelper.getInstance(mContext).getUid())) {

                        builder.setChooseThreeString(String.format(mContext.getString(R.string.report_unit), mItem.getCuname()))
                                .setChooseThreeClick(new OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ((QuestionActivity) mContext).gotoReportUser(position);
                                    }
                                });

                        String cuname = mItem.getCuname();
                        cuname = cuname.length() > 7 ? cuname.substring(0, 7) + "..." : cuname;
                        builder.setChooseFourString(String.format(mContext.getString(R.string.block_user_unit), cuname))
                                .setChooseFourClick(new OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ((QuestionActivity) mContext).doBlockUser(position);
                                    }
                                });
                    }
                    builder.create().show();

                    break;
                case R.id.question_item_accept_view:
                    ((QuestionActivity) mContext).gotoWriteAnswer(mItem);
                    break;
                case R.id.question_item_name_tv:
                case R.id.civ_ask_head_pic:
                    if (mItem.getAnonymous() == 0) {
                        gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, mItem.getCuid());
                    }
                    break;
                default:
                    break;
            }
        }
    }

}